package com.diet263.l.testlearn.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.TextView
import com.diet263.l.testlearn.R

/**
 * TODO: document your custom view class.
 */
class MyView : TextView {
    private var TAG = this::class.java.simpleName

    constructor(context: Context) : super(context) {
        init(null, 0)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        init(attrs, defStyle)
    }

    private fun init(attrs: AttributeSet?, defStyle: Int) {
        // Load attributes
        val a = context.obtainStyledAttributes(
            attrs, R.styleable.MyView, defStyle, 0
        )

        a.recycle()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.e(TAG,"TV dispatchTouchEvent " + ev?.action)
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.e(TAG,"TV onTouchEvent " + event?.action)
//        event?.let {
//            when (it.action) {
//                MotionEvent.ACTION_DOWN -> {
//                    text = getPosXY(event)
//                    return true
//                }
//                MotionEvent.ACTION_MOVE -> {
//                    text = getPosXY(event)
//                    return true
//                }
//                MotionEvent.ACTION_UP -> {
//                    text = getPosXY(event)
//                    return true
//                }
//                else -> return super.onTouchEvent(event)
//            }
//        }
        return super.onTouchEvent(event)
    }

    private fun getPosXY(event: MotionEvent) = "Pos tt : x " + event.x.toInt() + " y " + event.y.toInt()


}
